package com.yl.zqy.config.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yl.zqy.utils.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

import static com.yl.zqy.constant.ZQYConstant.*;

/**
 * @Description 填充器
 * @Author liang.yu
 * @Date 2024/3/31 14:03
 */
@Component
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        Map cache = ConvertUtils.getMemoryCacheByKey(CACHE_TYPE_CONFIG);
        String defaultPwd = cache.get(DEFAULT_PWD).toString();
        this.strictInsertFill(metaObject,"status",String.class,DEFAULT_SYS_STATUS_NORMAL);
        this.strictInsertFill(metaObject,"sort",Integer.class,DEFAULT_SYS_SORT_NORMAL);
        this.strictInsertFill(metaObject, "password", String.class, defaultPwd);
        this.strictInsertFill(metaObject, "createDate", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "creator", String.class, StpUtil.getLoginIdAsString());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update  fill ....");
        this.strictUpdateFill(metaObject, "updater", String.class, StpUtil.getLoginIdAsString());
        this.strictUpdateFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
    }
}
